//
//  KQMessageQuestionContentView.swift
//  KangYunYouJia
//
//  Created by zhouxiang on 2020/12/17.
//  Copyright © 2020 zhoux@zzkqdc.com. All rights reserved.
//

import UIKit
import KQGeneric

class KQMessageQuestionContentView: UIView, KQMessageContentViewType, UITableViewDataSource, UITableViewDelegate {
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        _commonInit()
    }
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _commonInit()
    }
    
    open func apply(_ message: KQMessageType) {
        _message = message
        guard let content = message.content as? KQMessageQuestionContent else {
            return
        }
        _delegate = content.delegate
        
        self.title = content.question
        self.list = content.children
        self.tableView?.reloadData()
        
    }
    
    /// 获取高度
    open class func getTotalHeight(title: String?, list: [KQMessageQuestionContent]?) -> CGFloat {
        let headerHeight = KQMessageQuestionContentHeader.getHeaderHeight(text: title)
        var totalHeight = headerHeight
        for model in (list ?? []) {
            let cellHeight = KQMessageQuestionContentCell.getCellHeight(model: model)
            totalHeight += cellHeight
        }
        return totalHeight
    }
    
    func _commonInit() {
        
        self.tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: 250, height: 80), style: .grouped)
        self.tableView?.separatorStyle = .none
        self.tableView?.dataSource = self
        self.tableView?.delegate = self
        self.tableView?.isUserInteractionEnabled = true
        self.tableView?.backgroundColor = .clear
        self.tableView?.isScrollEnabled = false
        self.tableView?.kq_registerCellNib(KQMessageQuestionContentCell.self)
        self.tableView?.kq_registerHeaderFooterNib(KQMessageQuestionContentHeader.self)
        self.addSubview(self.tableView!)
        
        self.tableView?.snp.makeConstraints({ (make) in
            make.edges.equalToSuperview()
        })
        
        self.isUserInteractionEnabled = true
        
    }
    
    weak var _delegate: KQMessageDelegate?
    private var _message: KQMessageType!
    var tableView: UITableView?
    var title: String?
    var list: [KQMessageQuestionContent]?
    
    // MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.kq_dequeueReusableCell(KQMessageQuestionContentCell.self)!
        let model = self.list?[indexPath.row]
        cell.setData(model: model)
        return cell
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = self.list?[indexPath.row]
        return KQMessageQuestionContentCell.getCellHeight(model: model)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return KQMessageQuestionContentHeader.getHeaderHeight(text: self.title)
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.kq_dequeueReusableHeaderFooter(KQMessageQuestionContentHeader.self)!
        header.setData(text: self.title)
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.list?[indexPath.row]
        let type = model?.questionMsgType?.code ?? ""
        if type == "1" {
            // 打电话
            _delegate?.tapCustom?(message: _message, nextType: .call, param: "\(model?.phone ?? "")")
        } else if type == "2" {
            // 跳转链接
            _delegate?.tapCustom?(message: _message, nextType: .jumpWebView, param: "\(model?.url ?? "")")
        } else {
            // 问答
            _delegate?.tapCustom?(message: _message, nextType: .pullAutoAnswer, param: String(model?.id?.int64Value ?? 0))
        }
        
    }

    
}
